<template>
  <div class="container">
    <div class="top">
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-input
        style="width:300px;"
        :disabled="true"
        placeholder="请搜索机构信息"
        v-model="organizationNameList.name"
      >
        <template slot="prepend">机构信息</template>
        <el-button slot="append" icon="el-icon-search" @click="organizationBtn"></el-button>
      </el-input>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
      >查询</el-button>
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
      >清空</el-button>
      <el-button type="info" size="small" icon="el-icon-tickets" @click="derive()">导出</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%" empty-text="暂无数据">
      <el-table-column prop="orgCode" label="机构编码" align="center"></el-table-column>
      <el-table-column prop="orgName" label="机构名称" align="center"></el-table-column>
      <el-table-column prop="money" label="销售金额" align="center"></el-table-column>
      <el-table-column prop="qty" label="销售数量" align="center"></el-table-column>
      <el-table-column prop="cost" label="销售成本" align="center"></el-table-column>
      <el-table-column prop="profit" label="销售毛利" align="center"></el-table-column>
      <el-table-column prop="profitRate" label="毛利率%" align="center"></el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination>
    <organizationSalesMarginCollectDialog
      :organizationDialog="organizationDialog"
      @tag="organizationTag"
    ></organizationSalesMarginCollectDialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import OrganizationSalesMarginCollectDialog from "@/components/salesMarginCollect/organizationSalesMarginCollectDialog";
import { orgController } from "@/api/salesMarginChart/salesMarginChartList";
import { getToken } from "@/utils/auth";
import { getnowDate } from "@/utils/index";

export default {
  components: {
    pagination: Pagination,
    organizationSalesMarginCollectDialog: OrganizationSalesMarginCollectDialog,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      date: [],
      search: {
        start_date: "",
        end_date: "",
      },
      organizationDialog: {
        centerDialogVisible: false,
      },
     
      organizationNameList: {
        name: "",
        id: "",
      },
        //默认时间
      normal_startime: getnowDate().yesterday,
      normal_endtime: getnowDate().endtimer,
    };
  },

  created() {
    this.search.start_date = this.normal_startime;
    this.search.end_date = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_date);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_date);
    }
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data =
        "&id=" +
        this.organizationNameList.id +
        "&startTime=" +
        this.search.start_date +
        "&endTime=" +
        this.search.end_date +
        "&pageSize=" +
        this.pageData.pageSize +
        "&currPage=" +
        this.pageData.page;
      orgController(data)
        .then((res) => {
          if (res.code == 0) {
            this.tableData = res.data.content;
            this.pageData.page = res.data.currentPage;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //超级机构搜索
    organizationBtn() {
      this.organizationDialog = {
        centerDialogVisible: true,
      };
    },
    organizationTag(data) {
      this.organizationNameList = {
        name: "",
        id: "",
      };
      let arrName = [];
      let arrId = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrId.push(res.id);
      });
      this.organizationNameList.name = arrName.join(",");
      this.organizationNameList.id = arrId.join(",");
    },
    // 本地搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_date = this.date[0];
        this.search.end_date = this.date[1];
      }
      this.pageData.total = null;
      this.pageData.page = 1;
      this.getData();
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //清空
    empty() {
      this.date = [];
       this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        start_date:this.normal_startime,
        end_date: this.normal_endtime,
      };
      this.organizationNameList = {
        name: "",
        id: "",
      };
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");

          link.href =
            this.$axios.defaults.baseURLs +
            "/depController/exportCsv?token=" +
            getToken() +
            "&id=" +
            this.organizationNameList.id +
            "&startTime=" +
            this.search.start_date +
            "&endTime=" +
            this.search.end_date;
            link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>